#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if(fl < 0)
    {
        std::cerr << "fctnl error" << std::endl;
        return;
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    while(true)
    {
        SetNonBlock(0);
        std::cout << "Please Enter# ";
        fflush(0);
        char buffer[1024];
        ssize_t n = read(0, buffer, sizeof(buffer)-1);
        if(n > 0)
        {
            buffer[n] = '\0';
            std::cout << "echo# " <<buffer << std::endl;
        }
        else if(n == 0)
        {
            std::cout << "read done" << std::endl;
            break;
        }
        else
        {
            if(errno == EWOULDBLOCK)
            {
                //do other thing
            }
            else
            {
                std::cerr << "read error" << std::endl;
                break;
            }
        }
    }
}